import { _decorator, Component, Node, UITransformComponent, EventTouch, Vec3, v3, Vec2, v2 } from "cc";
import { EventSystem } from "../../../../scripts/system/event_system/EventSystem";
const { ccclass, property } = _decorator;

@ccclass("ActorController")
export class ActorController extends EventSystem {
    /** 默认触摸事件节点 */
    @property(Node)
    eventNode: Node = null;

    /** 当前接收触摸事件节点 */
    curEventNode: Node = null;

    private _touchStartWorldPos: Vec2 = v2();
    private _touchCurWorldPos: Vec2 = v2();
    private _touchMoveDelta: Vec2 = v2();
    private _touchEndWorldPos: Vec2 = v2();

    private _touchStartHandler: Function = () => {};
    private _touchMoveHandler: Function = () => {};
    private _touchEndHandler: Function = () => {};

    onLoad(){
        let visibleSize = cc.view.getVisibleSize();
        this.initTouchHandler(this.eventNode as Node);
        this.eventNode.getComponent(UITransformComponent).width = visibleSize.width;
        this.eventNode.getComponent(UITransformComponent).height = visibleSize.height;
    }

    initTouchHandler(node: Node){
        node.on(Node.EventType.TOUCH_START, this.onTouchStart, this);
        node.on(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        node.on(Node.EventType.TOUCH_END, this.onTouchEnd, this);
        this.curEventNode = node;
    }

    offTouchHandler(){
        this.curEventNode.off(Node.EventType.TOUCH_START, this.onTouchStart, this);
        this.curEventNode.off(Node.EventType.TOUCH_MOVE, this.onTouchMove, this);
        this.curEventNode.off(Node.EventType.TOUCH_END, this.onTouchEnd, this);
    }

    onTouchStart(event: EventTouch){
        this._touchStartWorldPos = event.getLocation();
        this._touchStartHandler();
    }

    onTouchMove(event: EventTouch){
        this._touchCurWorldPos = event.getLocation();
        Vec2.subtract(this._touchMoveDelta, this._touchCurWorldPos, this._touchStartWorldPos);
        this._touchMoveHandler();
    }

    onTouchEnd(event: EventTouch){
        this._touchEndWorldPos = event.getLocation();
        this._touchEndHandler();
    }

    setTouchHandler(startHandler?: Function, moveHandler?: Function, endHandler?: Function){
        this._touchStartHandler = startHandler ? startHandler : () => { };
        this._touchMoveHandler = moveHandler ? moveHandler : () => { };
        this._touchEndHandler = endHandler ? endHandler : () => { };
    }

    resetAllHandler(){
        this._touchStartHandler = () => { };
        this._touchMoveHandler = () => { };
        this._touchEndHandler = () => { };
    }

    setTouchEventNode(node?: Node){
        if(node){
            this.offTouchHandler();
            this.initTouchHandler(node as Node);
        }else{
            this.initTouchHandler(this.eventNode as Node);
        }
    }

    getTouchStartWorldPos(){
        return this._touchStartWorldPos;
    }

    getTouchCurWorldPos(){
        return this._touchCurWorldPos;
    }

    getTouchMoveDelta(){
        return this._touchMoveDelta;
    }

    getTouchEndWorldPos(){
        return this._touchEndWorldPos;
    }
}
